/*
* 授权库
* date 2013-1-29
*/
var accessDialog={
	setting:{
		roleId:0,
		ajaxUrl:'',
		submitBtnId:'dialog_access_submit',
		checkAllBtnId:'dialog_access_check',
		treeId:'accessTree'
	},
	init:function(param){
		var that=this;
		that.setting=$.extend({},that.setting,param); 	
		that.addTemplateToDialog(that.template);
		that.makeTree();
		that.bindEvent();
		that.show();
	},
	addTemplateToDialog:function(html){
		linji_dialog.init({'title':'授权','html':html,'objWidth':'250'});
	},
	bindEvent:function(){
		var that=this;
		$('#'+that.setting.submitBtnId).on('click',function(){
			linji_dialog.setTitle('授权中');
			var zTree = $.fn.zTree.getZTreeObj(that.setting.treeId);
			var nodes = zTree.getCheckedNodes(true);
			var str='';
			for(var i=0,len=nodes.length;i<len;i++)
			{
				var pid=nodes[i]['pId']==null?'0':nodes[i]['pId'].toString();
				var temp=nodes[i]['id'].toString()+'-'+pid+'-'+(nodes[i]['level']+1).toString();
				str+='#'+temp;
			}
			str=str.substring(1);
			$.ajax({
				type:'POST',
				url:that.setting.ajaxUrl+'id/'+that.setting.roleId,
				data:'node='+str,
				success:function(data){
					if(data==1)
					{
						linji_dialog.setTitle('授权成功');
					}
				}
			});
		});
		$('#'+that.setting.checkAllBtnId).on('click',function(){
			var $this=$(this);
			var zTree = $.fn.zTree.getZTreeObj(that.setting.treeId);
			if($this.val()=='全选')
			{
				$this.val('取消');
				zTree.checkAllNodes(true);
			}else
				{
					$this.val('全选');
					zTree.checkAllNodes(false);
				}
		});		
	},
	makeTree:function(){
		var that=this;
		var setting = {
			check: {
				enable: true
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			async: {
				enable: true,
				type:'get',
				url: that.setting.ajaxUrl+'id/'+that.setting.roleId
			},
			callback: {
				beforeClick: function(){return false;}
			}						
		 };			 
		 $.fn.zTree.init($("#"+that.setting.treeId), setting, that.setting.nodes);	
	},
	show:function(){
		linji_dialog.show();
	},	
	template:'<div class="dialog_access">'
            	 +'<div class="dialog_access_content clearfix" id="dialog_access_content_value">'
				 +'<ul id="accessTree" class="ztree"></ul>'
                 +' </div>'
                 +'<div class="clearfix pt10">'
                	+'<input type="button" value="提交" class="public_button fr"  id="dialog_access_submit"/>'
					+'<input type="button" value="全选" class="public_button fr mr10"  id="dialog_access_check"/>'
                +'</div>'
           +' </div>'
};